<?php
/* SVN FILE: $Id: statistics_season_teams_controller.php 320 2010-07-14 13:56:02Z gregory.rome $ */
/**
 * StatisticsSeasonTeams Controller.
 *
 * cBBweb : Digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 * @version       $Revision: 320 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-07-14 13:56:02 +0000 (Wed, 14 Jul 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * StatisticsSeasonTeams Controller.
 *
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 */
class StatisticsSeasonTeamsController extends AppController {

	var $name = 'StatisticsSeasonTeams';
	var $paginate = array(
		'contain' => array(
    	'Team' => array(
    		'fields' => array('Team.ID', 'Team.strName', 'Team.iValue', 'Team.idCoach'),
    		'Race' => array('LocalizedString'),
    		'Coach')),
  		'order' => array('StatisticsSeasonTeam.iWins' => 'desc'));

	/* ------------------------------------------------------------------- */
	/**
	 * @todo Documentation
	 */
	function index() {
		if (!$this->championship) {
			$this->Session->setFlash(__('You must select a championship', true));
			$this->set('noview', true);
			return;
		} else {
			$this->set('noview', false);

			$statisticsSeasonTeams = $this->paginate('StatisticsSeasonTeam', array(
				'StatisticsSeasonTeam.bFinished' => 0,
				'StatisticsSeasonTeam.idRule_Listing' => $this->championship,
				'StatisticsSeasonTeam.iSeason' => $this->current['Championship']['iSeason']
			));

			$this->set('statisticsSeasonTeams', $statisticsSeasonTeams);
		}
	}

	/* ------------------------------------------------------------------- */
	/**
	 * @todo Documentation
	 */
	function get_bestteams($field='td',$limit=5,$season='current',$championship=false)
	{
		if(isset($this->passedArgs['championship'])) {
			$championship = $this->passedArgs['championship'];
		}
		if(isset($this->passedArgs['season'])) {
			$season = $this->passedArgs['season'];
		}
		if(isset($this->passedArgs['limit'])) {
			$limit = $this->passedArgs['limit'];
		}
		if(isset($this->passedArgs['field'])) {
			$field = $this->passedArgs['field'];
		}
		switch ($field) {
			case 'td':
				$field = 'Inflicted_iTouchdowns';
				$fieldName = __('TD', true);
				$fieldTitle = __('Touchdowns', true);
				break;
			case 'std':
				$field = 'Sustained_iTouchdowns';
				$fieldName = __('sTD', true);
				$fieldTitle = __('Sustained Touchdowns', true);
				$fieldOrder = 'asc';
				break;
			case 'pa':
				$field = 'Inflicted_iPasses';
				$fieldName = __('Pa', true);
				$fieldTitle = __('Passes', true);
				break;
			case 'spa':
				$field = 'Sustained_iPasses';
				$fieldName = __('sPa', true);
				$fieldTitle = __('Sustained Passes', true);
				$fieldOrder = 'asc';
				break;
			case 'inj':
				$field = 'Inflicted_iInjuries';
				$fieldName = __('Inj', true);
				$fieldTitle = __('Injuries', true);
				break;
			case 'mr':
				$field = 'Inflicted_iMetersRunning';
				$fieldName = __('MR', true);
				$fieldTitle = __('Meters Running', true);
				break;
			case 'cas':
				$field = 'Inflicted_iCasualties';
				$fieldName = __('Cas', true);
				$fieldTitle = __('Casualties', true);
				break;
			case 'dd':
				$field = 'Inflicted_iDead';
				$fieldName = __('Dd', true);
				$fieldTitle = __('Deads', true);
				break;
			case 'mp':
				$field = 'Inflicted_iMetersPassing';
				$fieldName = __('MP', true);
				$fieldTitle = __('Meters Passing', true);
				break;
			case 'ca':
				$field = 'Inflicted_iCatches';
				$fieldName = __('Ca', true);
				$fieldTitle = __('Catches', true);
				break;
			case 'sca':
				$field = 'Sustained_iCatches';
				$fieldName = __('sCa', true);
				$fieldTitle = __('Sustained Catches', true);
				$fieldOrder = 'asc';
				break;
			case 'int':
				$field = 'Inflicted_iInterceptions';
				$fieldName = __('Int', true);
				$fieldTitle = __('Interceptions', true);
				break;
			case 'sint':
				$field = 'Sustained_iInterceptions';
				$fieldName = __('sInt', true);
				$fieldTitle = __('Sustained Interceptions', true);
				break;
			case 'sinj':
				$field = 'Sustained_iInjuries';
				$fieldName = __('sInj', true);
				$fieldTitle = __('Sustained Injuries', true);
				$fieldOrder = 'asc';
				break;
			case 'scas':
				$field = 'Sustained_iCasualties';
				$fieldName = __('sCas', true);
				$fieldTitle = __('Sustained Casualties', true);
				$fieldOrder = 'asc';
				break;
			case 'sdd':
				$field = 'Sustained_iDead';
				$fieldName = __('sDd', true);
				$fieldTitle = __('Sustained Deads', true);
				$fieldOrder = 'asc';
				break;
			case 'smr':
				$field = 'Sustained_iMetersRunning';
				$fieldName = __('sMR', true);
				$fieldTitle = __('Sustained Meters Running', true);
				$fieldOrder = 'asc';
				break;
			case 'smp':
				$field = 'Sustained_iMetersPassing';
				$fieldName = __('sMP', true);
				$fieldTitle = __('Sustained Meters Passing', true);
				$fieldOrder = 'asc';
				break;
			case 'smp':
				$field = 'Sustained_iTouchdowns';
				$fieldName = __('sTD', true);
				$fieldTitle = __('Sustained Touchdowns', true);
				$fieldOrder = 'asc';
				break;
			default:
				$this->Session->setFlash(__('Wrong field', true));
				return;
		}

		if(!isset($fieldOrder)) {
			$fieldOrder = 'desc';
			$options['StatisticsSeasonTeam.'.$field.' <>'] = 0;
		}

		if( !$championship ) {
			$championship = $this->championship;
		}

		if ( $championship )
		{
			if($season == 'current') {
				$options['StatisticsSeasonTeam.bFinished'] = 0;
			} else {
				$options['StatisticsSeasonTeam.iSeason'] = $season;
			}
			$options['StatisticsSeasonTeam.idRule_Listing'] = $championship;
			$this->set('noview', false);
		} else {
			$this->Session->setFlash(__('You must select a championship', true));
			$this->set('noview', true);
			return;
		}

		$this->StatisticsSeasonTeam->recursive = 3;
		$statisticsSeasonTeams = $this->StatisticsSeasonTeam->find('all', array(
			'fields' => array('StatisticsSeasonTeam.'.$field, 'StatisticsSeasonTeam.idTeam_Listing'),
			'order' => array(
				'StatisticsSeasonTeam.'.$field => $fieldOrder,
				'Team.iValue' => 'desc'
				),
			'limit' => $limit,
			'contain' => array(
				'Team' => array(
					'fields' => array('Team.ID', 'Team.idRaces', 'Team.strName',
						'Team.idCoach','Team.iValue'),
					'Coach' => array('fields' => array('Coach.ID', 'Coach.strName')),
					'Race' => array(
						'LocalizedString'
						)
						)
						),
			'conditions' => $options,
						));

						if (isset ($this->params['requested'])) {
							return $statisticsSeasonTeams;
						} else {
							$this->set('statisticsSeasonTeams', $statisticsSeasonTeams);
							$this->set('field', $field);
							$this->set('fieldName', $fieldName);
							$this->set('fieldTitle', $fieldTitle);
						}
	}

	/* ------------------------------------------------------------------- */
	/** Get Worst Teams.
	 * @param field
	 * @param limit
	 */
	function get_worstteams() {
	}
}
?>